var utility = require('system/utility'),
    App = require('system/app'),
    mongodb = require('mongodb'),
    BSON = mongodb.BSONPure,
    HttpServer = require('system/httpserver');

var app = new App(
	{
		dataAccess: {
			mongo: {
				cadence: {
					address: '10.6.131.61',
					port: 27017,
					database: 'cadence',
					isDefault: true
				}
			},
			sqlServer: {
				cadence: {
					address: '10.6.131.61',
					port: 1433,
					userName: 'developer',
					password: 'developer',
					database: 'cadence',
					isDefault: true
				}
			}
		},
		controllers: [
			require('system/HttpProxyController'),
			require('system/StaticFileController'),
			require('contactInformation/entitiesController'),
			require('dataAccess/mongoController'),
			require('dataAccess/sqlServerController'),
			require('system/auditableController'),
			require('system/searchController'),
			require('security/sessionsController'),
			require('security/securedObjectsController'),
		//require('./dashboardController'),
			require('security/usersController')
		],
		routes: [
			{ name: 'dataAccess/storage/save', handler: 'dataAccess.mongo.save' },
			{ name: 'dataAccess/storage/find', handler: 'dataAccess.mongo.find' },
			{ name: 'dataAccess/storage/eval', handler: 'dataAccess.mongo.eval' },
			{ name: 'dataAccess/storage/beginTransaction', handler: 'dataAccess.mongo.beginTransaction' },
			{ name: 'dataAccess/storage/commitTransaction', handler: 'dataAccess.mongo.commitTransaction' },
			{ name: 'dataAccess/storage/rollbackTransaction', handler: 'dataAccess.mongo.rollbackTransaction' },
			{ name: 'dataAccess/sqlServer/save', handler: 'dataAccess.sqlServer.save' },
			{ name: 'dataAccess/sqlServer/find', handler: 'dataAccess.sqlServer.find' },
			{ name: 'dataAccess/sqlServer/eval', handler: 'dataAccess.sqlServer.eval' },
			{ name: 'dataAccess/sqlServer/beginTransaction', handler: 'dataAccess.sqlServer.beginTransaction' },
			{ name: 'dataAccess/sqlServer/commitTransaction', handler: 'dataAccess.sqlServer.commitTransaction' },
			{ name: 'dataAccess/sqlServer/rollbackTransaction', handler: 'dataAccess.sqlServer.rollbackTransaction' },
			{ name: 'security/sessions/getSession', handler: 'security.sessions.getSession' },
			{ name: 'security/sessions/set', handler: 'security.sessions.setSessionVariable' },
			{ name: 'security/sessions/find', handler: 'security.sessions.find' },
			{ name: 'security/sessions/findOne', handler: 'security.sessions.findOne' },
			{ name: 'security/sessions/exists', handler: 'security.sessions.exists' },
			{ name: 'security/sessions/getSession', handler: 'security.sessions.getSession' },
			{ name: 'security/users/save', handler: 'security.users.save' },
			{ name: '/security/users/search', handler: 'security.users.search' },
			{ name: 'security/users/findOne', handler: 'security.users.findOne' },
			{ name: 'security/users/exists', handler: 'security.users.exists' },
		//---------------------------------------------
			{name: '/contacts/entities/search', handler: 'contactInformation.entities.find' },
			{ name: '/data/search/test', handler: 'searchEngine.search.hub',
				data: {
					take: 10,
					_sources: [
						{
							name: "Contacts",
							path: '/contacts/entities/search',
							weight: 2,
							select: { name: 1, tags: 1 },
							where: {}
						},
						{
							name: "Users",
							path: '/security/users/search',
							weight: 1,
							select: { userName: 1 },
							where: {}
						}
					]
				}
			},
		//---------------------------------------------
			{name: '/dash/data/1', handler: 'dashboards.data', data: { sp: 'test1'} },
			{ name: '/test/', proxy: true, handler: 'files.static', data: { fileRoot: '?/../staticFiles/'} },
			{ name: '/c2/', proxy: true, handler: 'http.proxy', data: { host: 'localhost', port: 1600, _timeout: 90000, remotePath: '/'} },
			{ name: '/c3/', proxy: true, handler: 'http.proxy', data: { host: 'localhost', port: 1001, remotePath: '/'} }
		]
	}
);

var server = new HttpServer({
	app: app,
	port: parseInt(process.argv[2] || '8000'),
	address: '127.0.0.1',
	rootPath: process.cwd().replace(/\\/g,'/') + "/files",
	defaultPage: "/index.html"
});

server.start();
